//
//  TaishanRegisterViewController.m
//  Exam
//
//  Created by mac on 2019/6/27.
//  Copyright © 2019年 Changzhou Jinzhineng Software Co., Ltd. All rights reserved.
//

#import "TaishanRegisterViewController.h"
#import <Masonry.h>
#import "SizeUtil.h"
#import "ColorUtil.h"
#import "URLUtil.h"
#import "JZNRequestUtil.h"
#import "Toast+UIView.h"
#import <MJExtension.h>
#import "LoginViewController.h"

const float textLoginFont1 = 12;
const float buttonLoginFont1 = 13;

const float viewHeight1 = 45;
const float contentHeight1 = 30;

@interface TaishanRegisterViewController ()<UITextFieldDelegate>
@property(nonatomic, strong) UIImageView *logoImageView;
/**密码*/
@property(nonatomic, strong) UILabel *pwdLabel;
/**账号*/
@property(nonatomic, strong) UILabel *accountLabel;

/**密码*/
@property(nonatomic, strong) UILabel *nameLabel;

/**密码输入框*/
@property(nonatomic, strong) UITextField *pwdTextField;
/**账号输入框*/
@property(nonatomic, strong) UITextField *accountTextField;
/**账号输入框*/
@property(nonatomic, strong) UITextField *nameTextField;


/**注册按钮*/
@property(nonatomic, strong) UIButton *registerButton;
@end

@implementation TaishanRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"用户注册";
    
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [ColorUtil navigationBarColor:[ColorUtil titleColor] BarSize:bar.frame alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    [bar setTintColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    
    [self initView];
    // Do any additional setup after loading the view.
}
    
    /*
     #pragma mark - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     // Get the new view controller using [segue destinationViewController].
     // Pass the selected object to the new view controller.
     }
     */
    
-(void) regist {
    __block NSString *accountString = self.accountTextField.text;
    __block NSString *pwdString = self.pwdTextField.text;
    __block NSString *nameString = self.nameTextField.text;
    if (accountString == nil || [accountString isEqualToString:@""]) {
        [[UIApplication sharedApplication].delegate.window makeToast:@"请输入用户名"];
        return;
    }
    
    if (pwdString == nil || [pwdString isEqualToString:@""]) {
        [[UIApplication sharedApplication].delegate.window makeToast:@"请输入密码"];
        return;
    }
    
    if (nameString == nil || [nameString isEqualToString:@""]) {
        [[UIApplication sharedApplication].delegate.window makeToast:@"请输入姓名"];
        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@/user/regist" ,[URLUtil hostName]];
    NSDictionary *params = @{@"userMobile":accountString,
                             @"password":pwdString,
                             @"userName":nameString
                             };
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            LoginViewController *loginViewController = [[LoginViewController alloc]init];
            [UIApplication sharedApplication].delegate.window.rootViewController = loginViewController;
            [[UIApplication sharedApplication].delegate.window makeToast:@"注册成功，请登录"];
        }else {
            [[UIApplication sharedApplication].delegate.window makeToast:dataResult.msg];
        }
    } failure:^(NSError *error) {
        
    }];
}
    
# pragma mark ====页面控件====
-(void) initView{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.logoImageView];
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(100);
        make.centerX.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(50);
        }else {
            make.top.equalTo(self.view).offset(50);
        }
    }];
    
    [self.view addSubview:self.accountLabel];
    [self.accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(10*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.logoImageView.mas_bottom).offset(50);
    }];
    
    [self.view addSubview:self.accountTextField];
    [self.accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(contentHeight1*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.accountLabel.mas_bottom).offset(5);
    }];
    UIImageView *loginLine = [[UIImageView alloc]init];
    loginLine.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:loginLine];
    [loginLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(1);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.accountTextField.mas_bottom);
    }];
    
    
    [self.view addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(10*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(loginLine.mas_bottom).offset(10);
    }];
    [self.view addSubview:self.nameTextField];
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(contentHeight1*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
    }];
    
    UIImageView *nameLine = [[UIImageView alloc]init];
    nameLine.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:nameLine];
    [nameLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(1);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.nameTextField.mas_bottom);
    }];
    
    [self.view addSubview:self.pwdLabel];
    [self.pwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(10*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(nameLine.mas_bottom).offset(10);
    }];
    [self.view addSubview:self.pwdTextField];
    [self.pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(contentHeight1*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.pwdLabel.mas_bottom).offset(5);
    }];
    UIImageView *pwdLine = [[UIImageView alloc]init];
    pwdLine.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:pwdLine];
    [pwdLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(1);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.pwdTextField.mas_bottom);
    }];
    
    [self.view addSubview:self.registerButton];
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30*LSCALE);
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(pwdLine.mas_bottom).offset(20);
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
//通过委托来放弃“第一响应者”
#pragma mark - UITextView Delegate Method
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:
(NSRange)range replacementText:(NSString *)text
{
    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

# pragma mark ====控件====
-(UIImageView*) logoImageView{
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc]init];
        _logoImageView.image = [UIImage imageNamed:@"online_logo.png"];
    }
    return _logoImageView;
}
-(UILabel*) nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.text = @"姓名";
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.font = [UIFont systemFontOfSize:10*LSCALE];
    }
    return _nameLabel;
}
    
    
    
-(UILabel*) pwdLabel{
    if (!_pwdLabel) {
        _pwdLabel = [[UILabel alloc]init];
        _pwdLabel.text = @"密码";
        _pwdLabel.textColor = [UIColor blackColor];
        _pwdLabel.font = [UIFont systemFontOfSize:10*LSCALE];
    }
    return _pwdLabel;
}
    
-(UITextField*) pwdTextField{
    if (!_pwdTextField) {
        _pwdTextField = [[UITextField alloc]init];
        //        _pwdTextField.placeholder = @"6-16位字母、数字、符号组成";
        _pwdTextField.font = [UIFont systemFontOfSize:textLoginFont1*LSCALE];
        _pwdTextField.textColor = [UIColor blackColor];
        _pwdTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
        _pwdTextField.delegate = self;
        _pwdTextField.secureTextEntry = YES;
    }
    return _pwdTextField;
}
    
-(UILabel*) accountLabel{
    if (!_accountLabel) {
        _accountLabel = [[UILabel alloc]init];
        _accountLabel.text = @"账号";
        _accountLabel.textColor = [UIColor blackColor];
        _accountLabel.font = [UIFont systemFontOfSize:10*LSCALE];
    }
    return _accountLabel;
}
    
-(UITextField*) accountTextField{
    if (!_accountTextField) {
        _accountTextField = [[UITextField alloc]init];
        //        _accountTextField.placeholder = @"请输入手机号";
        _accountTextField.font = [UIFont systemFontOfSize:textLoginFont1*LSCALE];
        _accountTextField.textColor = [UIColor blackColor];
        _accountTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
        _accountTextField.delegate = self;
        // _accountTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _accountTextField;
}
    
-(UITextField*) nameTextField{
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc]init];
        //        _pwdTextField.placeholder = @"6-16位字母、数字、符号组成";
        _nameTextField.font = [UIFont systemFontOfSize:textLoginFont1*LSCALE];
        _nameTextField.textColor = [UIColor blackColor];
        _nameTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入姓名" attributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
        _nameTextField.delegate = self;
    }
    return _nameTextField;
}
    
-(UIButton*) registerButton{
    if (!_registerButton) {
        _registerButton = [[UIButton alloc]init];
        _registerButton.backgroundColor = [ColorUtil titleColor];
        _registerButton.titleLabel.font = [UIFont systemFontOfSize:buttonLoginFont1*LSCALE];
        _registerButton.titleLabel.textColor = [UIColor whiteColor];
        _registerButton.layer.cornerRadius = 5;
        [_registerButton setTitle:@"注  册" forState:UIControlStateNormal];
        [_registerButton addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerButton;
}

@end
